package com.liangzhong.proxy
{
	import com.adobe.crypto.MD5;
	import com.adobe.serialization.json.JSON;
	import com.global.GlobalDef;
	import com.liangzhong.constants.HttpRequestConst;
	import com.liangzhong.message.http.WebClient;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.User;
	
	import flash.net.URLRequest;
	import flash.net.URLVariables;

	/**
	 * 与短连接使用
	 * 固定的基本信息 
	 * @author Administrator
	 * 
	 */
	public class ProxyUtils
	{
		private static var _proxyutils:ProxyUtils;
		private var _user:User;
		private var _app:AppData
		public function ProxyUtils()
		{
			init();
		}
		private function init():void
		{
			_app = AppData.getInstance();
			_user = AppData.getInstance().user;
			
		}
		public static function get instance():ProxyUtils
		{
			if(_proxyutils == null)_proxyutils = new ProxyUtils();
			return _proxyutils;
		}
		/**
		 * uid
		 * <p>authkey</p>
		 * <p>cid 俱乐部Id</p>
		 * <p>format 默认为Reust_Format_Json :String = "json";</p>
		 * <p>time加密时间</p>
		 * @return 
		 * 
		 */
		public function getUrlVariables():URLVariables
		{
			var variablse:URLVariables = new URLVariables();
			variablse.uid = _user.uid;
			variablse.authkey = _user.key;
			variablse.cid = _user.uid;
			variablse.format = HttpRequestConst.Reust_Format_Json;
			variablse.time = _app.time
			return variablse;
			
		}
		/**
		 * Http 请求 入口
		 * @param $callBack 数据返回回调函数
		 * @param $variables 参数
		 * @param $isAddReqWaiting 是否限制操作
		 */
		public function webClient($callBack:Function,
								  $variables:URLVariables,
								  $isAddReqWaiting:Boolean = true):void
		{
			var web:WebClient = new WebClient();
			web.svrUrl = GlobalDef.Web_Api_Url;
			web.resultFormat =  $variables.format;
			web.isAddReqWaiting = $isAddReqWaiting;
			$variables.sig = MD5.hash("lz" + $variables.uid + $variables.time + $variables.method +"2012");
			web.call($callBack,$variables);
		}
		/**
		 *发送错误信息 
		 * @param $contents
		 * 
		 */
		public function sendErrorInfo($contents:Object):void
		{
			var variablse:URLVariables =getUrlVariables();
			variablse.method = "error.postErrorLog";
			variablse.data = $contents;
			webClient(getErrorInfo,variablse,false);
		}
		private function getErrorInfo($data:Object):void
		{
			trace("错误返回");
		}
		/**
		 *解析Http数据  
		 * 
		 */
		public function analysisHttpData($data:Object,$class:*):Object
		{
			///trace($class + $data);
			var str:String = String($data);
			var results:Object = JSON.decode(str);
			if(results == null)return null;
			return results[0];
		}
	}
}